﻿using System;
using System.Windows;
using Jacobi.Seesmic.FeedReader.PluginSettingsView;
using Jacobi.Seesmic.FeedReader.Utilities;
using Seesmic.Sdp.Extensibility;
using Seesmic.Sdp.Utils;

namespace Jacobi.Seesmic.FeedReader
{
    /// <summary>
    /// Implements the action for the 'Feed Reader' sidebar button.
    /// </summary>
    public sealed class ShowTimelineAction : ObservableBase, ISidebarAction
    {
        private Timeline _timeline;

        /// <summary>
        /// Constructs a new instance that is coupled to the specified <paramref name="timeline"/>.
        /// </summary>
        /// <param name="timeline">Must not be null.</param>
        internal ShowTimelineAction(Timeline timeline)
        {
            _timeline = timeline;
            _timeline.UnreadItemsChanged += TimelineUnreadItemsChanged;
        }

        /// <summary>
        /// Event handler to manage the <see cref="IsHighlighted"/> property.
        /// </summary>
        /// <param name="sender">The timeline sends the event.</param>
        /// <param name="e">Not used.</param>
        public void TimelineUnreadItemsChanged(object sender, EventArgs e)
        {
            IsHighlighted = (_timeline.UnreadItemsCount != 0);
        }

        /// <summary>
        /// Returns the small feed image.
        /// </summary>
        public DataTemplate Icon
        {
            get { return PluginSettingsViews.SmallFeedImage; }
        }

        /// <summary>
        /// Called by the host when the user clicks on the sidebar button.
        /// </summary>
        /// <param name="context">Should never be null.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods",
            MessageId = "0", Justification = "Guard")]
        public void Invoke(SidebarActionContext context)
        {
            context.ThrowIfArgumentIsNull("context");

            context.Space.AddTimelineColumn(this, _timeline, Text, Icon);
        }

        private bool _isHighlighted;

        /// <summary>
        /// Indicates if the Timeline this action activates has new items.
        /// </summary>
        public bool IsHighlighted
        {
            get { return _isHighlighted; }
            private set
            {
                SetPropertyValue(ref _isHighlighted, value, "IsHighlighted");
            }
        }

        /// <summary>
        /// Returns the unique plugin Id.
        /// </summary>
        public string PersistId
        {
            get { return Plugin.UniqueId.ToString(); }
        }

        /// <summary>
        /// Gets the text to be displayed in the sidebar button.
        /// </summary>
        public string Text
        {
            get { return "Feed Reader"; }
        }
    }
}